import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'LoginView',
    component: () => import('@/components/Login/LoginView.vue')
  },
  {
    path: '/home',
    name: 'HomeView',
    component: () => import('@/views/HomeView/HomeView.vue'),
    redirect: '/welcome',
    children: [
      {
        path: '/welcome',
        name: 'welcome',
        component: () => import('@/components/welcome.vue'),
        meta: {
          titleName: 'welcome',
          sonName: ''
        }
      },
      {
        path: '/users',
        name: 'UsersView',
        component: () => import('@/components/User/UsersView/UserView.vue'),
        meta: {
          titleName: '用户管理',
          sonName: '用户列表'

        }
      },
      {
        path: '/roles',
        name: 'RolesView',
        component: () => import('@/components/Rights/RolesView/RolesView.vue'),
        meta: {
          titleName: '权限管理',
          sonName: '角色列表'

        }
      },
      {
        path: '/rights',
        name: 'RightsView',
        component: () => import('@/components/Rights/RightsView/RightsView.vue'),
        meta: {
          titleName: '权限管理',
          sonName: '权限列表'

        }
      },
      {
        path: '/goods',
        name: 'GoodsView',
        component: () => import('@/components/Goods/GoodsView/GoodsView.vue'),
        meta: {
          titleName: '商品管理',
          sonName: '商品列表'
        }
      },
      {
        path: '/params',
        name: 'ParamsView',
        component: () => import('@/components/Goods/ParamsView/ParamsView.vue'),
        meta: {
          titleName: '商品管理',
          sonName: '商品参数'

        }
      },
      {
        path: '/categories',
        name: 'CategoriesView',
        component: () => import('@/components/Goods/CategoriesView/CategoriesView.vue'),
        meta: {
          titleName: '商品管理',
          sonName: '商品分类'

        }
      },
      {
        path: '/orders',
        name: 'OrdersView',
        component: () => import('@/components/Orders/OrdersView/OrdersView.vue'),
        meta: {
          titleName: '订单管理',
          sonName: '订单列表'

        }
      },
      {
        path: '/reports',
        name: 'ReportsView',
        component: () => import('@/components/Reports/ReportsView/ReportsView.vue'),
        meta: {
          titleName: '数据统计',
          sonName: '数据报表'
        }
      },
      {
        path: '/goods/addGoods',
        name: 'AddGoods',
        component: () => import('@/components/Goods/GoodsView/addGoods.vue'),
        meta: {
          titleName: '商品管理',
          sonName: '添加商品'
        }
      }
    ]
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach(
  (to, from, next) => {
    const token = window.localStorage.getItem('token')
    console.log(to.path)
    if (!token) {
      if (to.path === '/login') {
        return next()
      } else {
        return next('/login')
      }
    } else {
      next()
    }
  }
)

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (location) {
  return originalPush.call(this, location).catch(err => err)
}

export default router
